package com.xiaoyy.core.dict.integration.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoyy.core.common.cache.CacheConst;
import com.xiaoyy.core.common.enums.BoolEnum;
import com.xiaoyy.core.common.enums.FuncTypeEnum;
import com.xiaoyy.core.common.exception.AppException;
import com.xiaoyy.core.common.utils.AntTreeUtil;
import com.xiaoyy.core.common.vo.AntTreeVO;
import com.xiaoyy.core.common.vo.DictDeleteVO;
import com.xiaoyy.core.common.vo.DictVO;
import com.xiaoyy.core.common.vo.SelectVO;
import com.xiaoyy.core.config.annotations.OpApi;
import com.xiaoyy.core.config.util.PagerUtil;
import com.xiaoyy.core.config.util.RedisUtil;
import com.xiaoyy.core.dict.dao.model.SysDict;
import com.xiaoyy.core.dict.dao.model.SysOrg;
import com.xiaoyy.core.dict.integration.DictIntegration;
import com.xiaoyy.core.dict.service.SysDictService;
import com.xiaoyy.core.dict.service.DictOrgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据字典服务
 * */
@Service
public class DictIntegrationImpl implements DictIntegration {
    @Autowired
    SysDictService sysDictService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    DictOrgService dictOrgService;

    /**
     * 根据字段和key获取值
     * */
    @Override
    @OpApi(funcCode="DICT0001",title = "获取数据字典值",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public String getValue(String dictField, String dictKey) {
        if (StringUtils.isBlank(dictField)|| StringUtils.isBlank(dictKey)){
            return "";
        }
        List<SelectVO> selectVOList = getDict(dictField);
        if (CollectionUtils.isEmpty(selectVOList)){
            return "";
        }
        Map<String, String> map = selectVOList.stream().collect(Collectors.toMap(SelectVO::getDictKey, SelectVO::getDictValue,(k1, k2)->k1));
        return map.get(dictKey);
    }
    /**
     * 根据字段获取字典值
     * @param dictField 字段
     * */
    @Override
    @OpApi(funcCode="DICT0002",title = "单个获取字段的下拉数据字典",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public List<SelectVO> getDict(String dictField) {
        if (StringUtils.isBlank(dictField)){
            return new ArrayList<>();
        }
        String key =dictField.toUpperCase();
        List<SelectVO> selectVOList = new ArrayList<>();
        if (redisUtil.hHasKey(CacheConst.SYS_DICT_VALUE, key)){
            selectVOList = (List<SelectVO>) redisUtil.hget(CacheConst.SYS_DICT_VALUE, key);
        }else{
            QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysDict::getDel, BoolEnum.FALSE.getType())
                    .eq(SysDict::getStatus,BoolEnum.TRUE.getType())
                    .eq(SysDict::getDictField,key);

            List<SysDict> sysDicts=sysDictService.list(queryWrapper);

            if (!CollectionUtils.isEmpty(sysDicts)){
                selectVOList =  Convert.toList(SelectVO.class,sysDicts);
                redisUtil.hset(CacheConst.SYS_DICT_VALUE, key,selectVOList,172800);
            }
        }
        return selectVOList;
    }
    /**
     * 根据字段获取批量字典值
     * @param dictFields 批量字段，用英文逗号隔开
     * */
    @Override
    @OpApi(funcCode="DICT0003",title = "批量获取字段的下拉数据字典",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public Map<String,List<SelectVO>> getDictBatch(String dictFields) {
        Map<String,List<SelectVO>> map = new HashMap<>();
        if (StringUtils.isBlank(dictFields)){
            return map;
        }
        List<String> dictFieldList = Arrays.stream(dictFields.split(",")).map(s -> s).collect(Collectors.toList());
        for (String dictField:dictFieldList){
            map.put(dictField,getDict(dictField));
        }
        return map;
    }

    /**
     * 根据条件获取数据字典列表
     * @param dictVO
     * */
    @Override
    @OpApi(funcCode="DICT0004",title = "根据条件查询数据字典列表",funcType= FuncTypeEnum.query)
    public PageInfo<DictVO> getDictList(DictVO dictVO) {
        SysDict query = Convert.convert(SysDict.class,dictVO);
        if (StringUtils.isBlank(query.getDel())) {
            query.setDel(BoolEnum.FALSE.getType());
        }
        if (StringUtils.isBlank(query.getStatus())) {
            query.setStatus(BoolEnum.TRUE.getType());
        }
        Integer pageNum = dictVO.getPageNum();
        Integer pageSize = dictVO.getPageSize();
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<DictVO> list = sysDictService.selectPageList(query);
        if (CollectionUtils.isEmpty(list)) {
            return PagerUtil.createEmptyPagerInfo(pageNum, pageSize);
        }
        PageInfo<DictVO> dictPageInfo = new PageInfo<>(list);
        return dictPageInfo;
    }

    @Override
    @OpApi(funcCode="DICT0005",title = "获取数据字典详情",funcType= FuncTypeEnum.query)
    public DictVO getDictDetail(DictVO dictVO) {
        Long sysId = dictVO.getSysId();
        if (sysId==null){
            throw new AppException("字典id不能为空");
        }
        SysDict query = new SysDict();
        query.setSysId(sysId);
        query.setDel(BoolEnum.FALSE.getType());
        List<DictVO> list = sysDictService.selectPageList(query);
        if (CollectionUtils.isEmpty(list)) {
            throw new AppException("找不到数据字典信息");
        }
        return list.get(0);
    }

    @Override
    @OpApi(funcCode="DICT0006",title = "获取所有数据字典字段",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public List<SelectVO> getDictField() {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(SysDict::getDictField,SysDict::getFieldName)
                .groupBy(SysDict::getDictField).groupBy(SysDict::getFieldName);
        List<SysDict> sysDicts=sysDictService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(sysDicts)){
            return Convert.toList(SelectVO.class,sysDicts);
        }else {
            return new ArrayList<>();
        }

    }

    @Override
    @Transactional
    @OpApi(funcCode="DICT0007",title = "新增数据字典信息",funcType= FuncTypeEnum.insert)
    public DictVO add(DictVO dictVO) {
        if (dictVO.getSysId()!=null){
            throw new AppException("新增字典Id必须为空");
        }
        //校验
        checkDict(dictVO,null);

        SysDict sysDict = Convert.convert(SysDict.class,dictVO);
        if (StringUtils.isNotBlank(dictVO.getOrgId())){
            sysDict.setOrgNumber(getOrgCodeById(dictVO.getOrgId()));
        }
        sysDict.setDictField(sysDict.getDictField().toUpperCase());
        updateFieldName(dictVO);
        sysDictService.save(sysDict);
        dictVO.setSysId(sysDict.getSysId());
        this.clearCache(sysDict.getDictField());
        return getDictDetail(dictVO);
    }
    /**
     * 校验重复
     * */
    private void checkDict(DictVO dictVO,SysDict oldDict){
        if (StringUtils.isBlank(dictVO.getParentField())){
            if (dictVO.getDictKey().equals(dictVO.getParentKey())){
                throw new AppException("不能将自己作为父字典值");
            }
        }else{
            if (dictVO.getParentField().equals(dictVO.getDictField())){
                if (dictVO.getDictKey().equals(dictVO.getParentKey())){
                    throw new AppException("不能将自己作为父字典值");
                }
            }
        }
        boolean keyFlag = true;
        boolean valueFlag = true;
        if (oldDict!=null){
            if (oldDict.getDictField().equals(dictVO.getDictField())){
                if (oldDict.getDictKey().equals(dictVO.getDictKey())){
                    keyFlag= false;
                }
                if (oldDict.getDictValue().equals(dictVO.getDictValue())){
                    valueFlag= false;
                }
            }
        }
        if (keyFlag){
            QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysDict::getDel, BoolEnum.FALSE.getType())
                    .eq(SysDict::getDictKey,dictVO.getDictKey())
                    .eq(SysDict::getDictField,dictVO.getDictField().toUpperCase());
            int count = sysDictService.count(queryWrapper);
            if (count>0){
                throw new AppException("重复数据字典值");
            }
        }
        if (valueFlag){
            QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysDict::getDel, BoolEnum.FALSE.getType())
                    .eq(SysDict::getDictValue,dictVO.getDictValue())
                    .eq(SysDict::getDictField,dictVO.getDictField().toUpperCase());
            int count = sysDictService.count(queryWrapper);
            if (count>0){
                throw new AppException("重复数据字典值名称");
            }
        }
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDict::getDel, BoolEnum.FALSE.getType())
                .eq(SysDict::getFieldName,dictVO.getFieldName())
                .eq(SysDict::getDictField,dictVO.getDictField().toUpperCase());
        int count = sysDictService.count(queryWrapper);
        if (count>0){
            throw new AppException("重复字典字段名称");
        }
    }

    private void updateFieldName(DictVO dictVO){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(SysDict::getFieldName).eq(SysDict::getDel, BoolEnum.FALSE.getType())
                .eq(SysDict::getDictField,dictVO.getDictField().toUpperCase()).groupBy(SysDict::getFieldName);
        List<SysDict> list = sysDictService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            SysDict sysDict = list.get(0);
            if (!sysDict.getFieldName().equals(dictVO.getFieldName())){
                UpdateWrapper<SysDict> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().set(SysDict::getFieldName,dictVO.getFieldName())
                        .eq(SysDict::getDictField,dictVO.getDictField().toUpperCase())
                        .eq(SysDict::getDel,BoolEnum.FALSE.getType());
                sysDictService.update(updateWrapper);
            }

        }
    }
    @Override
    @Transactional
    @OpApi(funcCode="DICT0008",title = "修改数据字典信息",funcType= FuncTypeEnum.update)
    public DictVO edit(DictVO dictVO) {
        SysDict oldDict = sysDictService.getById(dictVO.getSysId());
        if (oldDict==null){
            throw new AppException("未找到需要修改的数据");
        }
        if (BoolEnum.TRUE.getType().equals(oldDict.getDel())){
            throw new AppException("该数据已被删除");
        }
        //校验
        checkDict(dictVO,oldDict);
        SysDict newDict = Convert.convert(SysDict.class,oldDict);
        BeanUtil.copyProperties(dictVO,newDict);
        updateFieldName(dictVO);
        if (StringUtils.isNotBlank(dictVO.getOrgId())){
            newDict.setOrgNumber(getOrgCodeById(dictVO.getOrgId()));
        }
        sysDictService.updateById(newDict);
        this.clearCache(oldDict.getDictField());
        if (!dictVO.getDictField().equals(oldDict.getDictField())){
            this.clearCache(dictVO.getDictField());
        }
        return getDictDetail(dictVO);
    }

    @Override
    @OpApi(funcCode="DICT0009",title = "单个删除数据字典信息",funcType= FuncTypeEnum.delete)
    public void delete(DictDeleteVO dictVO) {
        SysDict sysDict = sysDictService.getById(dictVO.getSysId());
        if (sysDict==null){
            throw new AppException("未找到需要删除的数据");
        }
        if (BoolEnum.TRUE.getType().equals(sysDict.getDel())){
            throw new AppException("该数据已被删除");
        }
        UpdateWrapper<SysDict> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(SysDict::getDel,BoolEnum.TRUE.getType())
                .eq(SysDict::getSysId,dictVO.getSysId());
        sysDictService.update(updateWrapper);
        this.clearCache(sysDict.getDictField());
    }

    @Override
    @OpApi(funcCode="DICT0010",title = "批量删除数据字典信息",funcType= FuncTypeEnum.delete)
    public void deleteBatch(DictDeleteVO dictVO) {
        if (CollectionUtils.isEmpty(dictVO.getSysIds())){
            throw new AppException("字典ID不能传空");
        }
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SysDict::getSysId,dictVO.getSysIds())
                .eq(SysDict::getDel,BoolEnum.FALSE.getType());
        List<SysDict> list = sysDictService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)){
            throw new AppException("未找到需要删除的数据");
        }
        List<String> dictFields = new ArrayList<>();
        for(SysDict sysDict :list){
            sysDict.setDel(BoolEnum.TRUE.getType());
            if (!dictFields.contains(sysDict.getDictField())){
                dictFields.add(sysDict.getDictField());
            }
        }
        sysDictService.updateBatchById(list);
        if (!CollectionUtils.isEmpty(dictFields)){
            for(String dictField:dictFields){
                this.clearCache(dictField);
            }
        }
    }

    @Override
    @OpApi(funcCode="DICT0011",title = "清除数据字典缓存",funcType= FuncTypeEnum.other)
    public void clearCache(String dictField) {
        if (StringUtils.isNotBlank(dictField)){
            dictField = dictField.toUpperCase();
            redisUtil.hdel(CacheConst.SYS_DICT_VALUE, dictField);
            redisUtil.hdel(CacheConst.SYS_ALL_DICT_COMMON_TREE, dictField.toUpperCase());
            if (dictField.equalsIgnoreCase("AREA")){
                redisUtil.del(CacheConst.SYS_ALL_AREA_TREE);
            }
            if (dictField.equalsIgnoreCase("ICON")){
                redisUtil.del(CacheConst.SYS_ALL_ICON_TREE);
            }
            if (dictField.equalsIgnoreCase("ORGTYPE")){
                redisUtil.del(CacheConst.SYS_ALL_ORG_TYPE_TREE);
            }
        }else {
            redisUtil.del(CacheConst.SYS_DICT_VALUE);
            redisUtil.del(CacheConst.SYS_ALL_ORG_TYPE_TREE);
            redisUtil.del(CacheConst.SYS_ALL_ICON_TREE);
            redisUtil.del(CacheConst.SYS_ALL_DICT_COMMON_TREE);
            redisUtil.del(CacheConst.SYS_ALL_AREA_TREE);
            redisUtil.del(CacheConst.SYS_DICT_ORG_NAME_ID);
            redisUtil.del(CacheConst.SYS_DICT_ORG_NAME_CODE);
            redisUtil.del(CacheConst.SYS_DICT_ORG_ID_CODE);
            redisUtil.del(CacheConst.SYS_DICT_ORG_CODE_ID);
        }
    }

    @Override
    @OpApi(funcCode="DICT0012",title = "根据组织id获取名称",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public String getOrgNameById(String orgId) {
        if (StringUtils.isBlank(orgId)){
            return "";
        }
        Map<Object,Object> map = new HashMap<>();
        if (redisUtil.hasKey(CacheConst.SYS_DICT_ORG_NAME_ID)){
            map = redisUtil.hmget(CacheConst.SYS_DICT_ORG_NAME_ID);
        }else{
            map.putAll(setCacheOrgMap("1"));
        }
        if (map.get(orgId)!=null){
            return map.get(orgId).toString();
        }else{
            return "";
        }
    }

    @Override
    @OpApi(funcCode="DICT0013",title = "根据组织编码获取名称",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public String getOrgNameByCode(String orgCode) {
        if (StringUtils.isBlank(orgCode)){
            return "";
        }
        Map<Object,Object> map = new HashMap<>();
        if (redisUtil.hasKey(CacheConst.SYS_DICT_ORG_NAME_CODE)){
            map = redisUtil.hmget(CacheConst.SYS_DICT_ORG_NAME_CODE);
        }else{
            map.putAll(setCacheOrgMap("2"));
        }
        if (map.get(orgCode)!=null){
            return map.get(orgCode).toString();
        }else{
            return "";
        }
    }

    @Override
    @OpApi(funcCode="DICT0014",title = "根据组织编码获取ID",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public String getOrgIdByCode(String orgCode) {
        if (StringUtils.isBlank(orgCode)){
            return "";
        }
        Map<Object,Object> map = new HashMap<>();
        if (redisUtil.hasKey(CacheConst.SYS_DICT_ORG_ID_CODE)){
            map = redisUtil.hmget(CacheConst.SYS_DICT_ORG_ID_CODE);
        }else{
            map.putAll(setCacheOrgMap("3"));
        }
        if (map.get(orgCode)!=null){
            return map.get(orgCode).toString();
        }else{
            return "";
        }
    }

    @Override
    @OpApi(funcCode="DICT0015",title = "根据组织id获取编码",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public String getOrgCodeById(String orgId) {
        if (StringUtils.isBlank(orgId)){
            return "";
        }
        Map<Object,Object> map = new HashMap<>();
        if (redisUtil.hasKey(CacheConst.SYS_DICT_ORG_CODE_ID)){
            map = redisUtil.hmget(CacheConst.SYS_DICT_ORG_CODE_ID);
        }else{
            map.putAll(setCacheOrgMap("4"));
        }
        if (map.get(orgId)!=null){
            return map.get(orgId).toString();
        }else{
            return "";
        }
    }

    private Map<String, Object> setCacheOrgMap(String OrgMapType) {
        QueryWrapper<SysOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysOrg::getDel,BoolEnum.FALSE.getType());
        List<SysOrg> sysOrgList = dictOrgService.list(queryWrapper);
        if (CollectionUtils.isEmpty(sysOrgList)) {
            return new HashMap<>();
        }
        Map<String, Object> nameIdMap = sysOrgList.stream().collect(Collectors.toMap(SysOrg::getOrgId, SysOrg::getOrgName, (k1, k2) -> k1));
        redisUtil.hmset(CacheConst.SYS_DICT_ORG_NAME_ID, nameIdMap, 86400);
        Map<String, Object> nameCodeMap = sysOrgList.stream().collect(Collectors.toMap(SysOrg::getOrgCode, SysOrg::getOrgName, (k1, k2) -> k1));
        redisUtil.hmset(CacheConst.SYS_DICT_ORG_NAME_CODE, nameCodeMap, 86400);
        Map<String, Object> idCodeMap = sysOrgList.stream().collect(Collectors.toMap(SysOrg::getOrgCode, SysOrg::getOrgId, (k1, k2) -> k1));
        redisUtil.hmset(CacheConst.SYS_DICT_ORG_ID_CODE, idCodeMap, 86400);
        Map<String, Object> codeIdMap = sysOrgList.stream().collect(Collectors.toMap(SysOrg::getOrgId, SysOrg::getOrgCode, (k1, k2) -> k1));
        redisUtil.hmset(CacheConst.SYS_DICT_ORG_CODE_ID, codeIdMap, 86400);
        if ("1".equals(OrgMapType)) {
            return nameIdMap;
        } else if ("2".equals(OrgMapType)) {
            return nameCodeMap;
        } else if ("3".equals(OrgMapType)) {
            return idCodeMap;
        } else if ("4".equals(OrgMapType)) {
            return codeIdMap;
        }
        return new HashMap<>();
    }
    @Override
    @OpApi(funcCode="DICT0016",title = "获取通用数据字典树",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public List<AntTreeVO> getDictCommonTree(String dictField) {
        if (StringUtils.isBlank(dictField)){
            return new ArrayList<>();
        }
        dictField  = dictField.toUpperCase();
        List<AntTreeVO> antTreeVOS = new ArrayList<>();
        if (redisUtil.hHasKey(CacheConst.SYS_ALL_DICT_COMMON_TREE,dictField)){
            antTreeVOS = (List<AntTreeVO>) redisUtil.hget(CacheConst.SYS_ALL_DICT_COMMON_TREE,dictField);
            return antTreeVOS;
        }
        DictVO query = new DictVO();
        query.setDictField(dictField);
        query.setStatus(BoolEnum.TRUE.getType());
        List<DictVO> dictVOList=this.getDictList(query).getList();
        Map<String,Object> map  = new HashMap<>();
        map.put("title","title");
        for (DictVO dictVO:dictVOList){
            AntTreeVO antTreeVO = new AntTreeVO();
            antTreeVO.setKey(dictVO.getDictKey());
            //选择后获取的值
            antTreeVO.setValue(dictVO.getDictKey());
            antTreeVO.setParentKey(dictVO.getParentKey());
            antTreeVO.setTitle(dictVO.getDictValue());
            antTreeVO.setScopedSlots(map);
            antTreeVOS.add(antTreeVO);
        }

        antTreeVOS = AntTreeUtil.constructTaskDTOToTree(antTreeVOS);
        //进缓存
        redisUtil.hset(CacheConst.SYS_ALL_DICT_COMMON_TREE,dictField,antTreeVOS,86400);
        return antTreeVOS;
    }
    /**
     * indata和notIndata 只能到省级
     * */
    @Override
    @OpApi(funcCode="DICT0017",title = "获取全国行政区划字典树",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public List<AntTreeVO> getAreaSelectTree(List<String> inData, List<String> notInData) {
        if (!CollectionUtils.isEmpty(inData)&&!CollectionUtils.isEmpty(inData)){
            throw new AppException("包含和剔除只能选择一种");
        }
        String key ="all";
        if (!CollectionUtils.isEmpty(inData)){
            key = JSON.toJSONString(inData);
        }
        if (!CollectionUtils.isEmpty(notInData)){
            key = JSON.toJSONString(notInData);
        }
        key = SecureUtil.md5(key);
        List<AntTreeVO> antTreeVOS = new ArrayList<>();
        if (redisUtil.hHasKey(CacheConst.SYS_ALL_AREA_TREE,key)){
            antTreeVOS = (List<AntTreeVO>) redisUtil.hget(CacheConst.SYS_ALL_AREA_TREE,key);
            return antTreeVOS;
        }
        DictVO query = new DictVO();
        query.setDictField("AREA");
        query.setStatus(BoolEnum.TRUE.getType());
        List<DictVO> dictVOList=this.getDictList(query).getList();
        if (CollectionUtils.isEmpty(dictVOList)){
            return new ArrayList<>();
        }
        Map<String,Object> map  = new HashMap<>();
        map.put("title","title");
        for (DictVO dictVO:dictVOList){
            AntTreeVO antTreeVO = new AntTreeVO();
            antTreeVO.setKey(dictVO.getDictKey());
            //选择后获取的值
            antTreeVO.setValue(dictVO.getDictKey());
            antTreeVO.setParentKey(dictVO.getParentKey());
            antTreeVO.setTitle(dictVO.getDictValue());
            antTreeVO.setScopedSlots(map);
            antTreeVOS.add(antTreeVO);
        }

        antTreeVOS = AntTreeUtil.constructTaskDTOToTree(antTreeVOS);
        List<AntTreeVO> retList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inData)){
            for (AntTreeVO antTreeVO:antTreeVOS){
                if (inData.contains(antTreeVO.getKey())){
                    retList.add(antTreeVO);
                }
            }
        }
        if (!CollectionUtils.isEmpty(notInData)){
            for (AntTreeVO antTreeVO:antTreeVOS){
                if (!notInData.contains(antTreeVO.getKey())){
                    retList.add(antTreeVO);
                }
            }
        }
        //进缓存
        redisUtil.hset(CacheConst.SYS_ALL_AREA_TREE,key,retList,86400);
        return retList;
    }

    /**
     * 图标下拉树
     * */
    @Override
    @OpApi(funcCode="DICT0018",title = "获取图标下拉树",funcType= FuncTypeEnum.query,publicFlag = BoolEnum.TRUE)
    public List<AntTreeVO> getIconSelectTree() {
        List<AntTreeVO> antTreeVOS = new ArrayList<>();
        if (redisUtil.hasKey(CacheConst.SYS_ALL_ICON_TREE)){
            antTreeVOS = (List<AntTreeVO>) redisUtil.get(CacheConst.SYS_ALL_ICON_TREE);
            return antTreeVOS;
        }
        DictVO query = new DictVO();
        query.setDictField("ICON");
        query.setStatus(BoolEnum.TRUE.getType());
        List<DictVO> dictVOList=this.getDictList(query).getList();
        Map<String,Object> map  = new HashMap<>();
        map.put("title","title");
        for (DictVO dictVO:dictVOList){
            AntTreeVO antTreeVO = new AntTreeVO();
            antTreeVO.setKey(dictVO.getDictKey());
            //选择后获取的值
            antTreeVO.setValue(dictVO.getDictKey());
            antTreeVO.setParentKey(dictVO.getParentKey());
            antTreeVO.setTitle(dictVO.getDictValue());
            if (!"leaf".equalsIgnoreCase(dictVO.getClassify())){
                antTreeVO.setSelectable(false);
                antTreeVO.setDisabled(true);
                antTreeVO.setDisableCheckbox(true);
                antTreeVO.setTitle(dictVO.getDictValue());
            }
            antTreeVO.setScopedSlots(map);
            antTreeVOS.add(antTreeVO);
        }

        antTreeVOS = AntTreeUtil.constructTaskDTOToTree(antTreeVOS);
        //进缓存
        redisUtil.set(CacheConst.SYS_ALL_ICON_TREE,antTreeVOS,86400);
        return antTreeVOS;
    }
}
